<?php

namespace common\service;

use common\model\CosMediaModel;
use common\model\MediaModel;
use common\model\NoteModel;
use common\model\NoteRecordModel;
use common\model\UserModel;
use support\SingletonTrait;

class NoteRecordService
{
    // 单例工具
    use SingletonTrait;

    /**
     * 需求列表
     * @param $filter
     * @param $page
     * @return array
     */
    public function getList($filter, $page)
    {
        $noteRecordList = model(NoteRecordModel::class)
            ->where($filter)
            ->page($page[0], $page[1])
            ->order('id desc')
            ->select();
        if (empty($noteRecordList)) {
            return [];
        }
        $userIds = array_column($noteRecordList, 'created_by');
        $userList = model(UserModel::class)->where(['id' => ['IN', $userIds]])->field('id,name,avatar')->select();
        if (empty($userList)) {
            return [];
        }
        $medias = [];
        $cosMedias = [];
        foreach ($noteRecordList as $item) {
            $item['update_json'] = json_decode($item['update_json'], true);
            if (!empty($item['update_json'])) {
                $noteMedia = [];
                if (!empty($item['update_json']['note_media'])) {
                    $noteMedia = explode(',', $item['update_json']['note_media']);
                }
                $medias = array_merge($medias, $noteMedia);
                $noteCosMedia = [];
                if (!empty($item['update_json']['note_cos_media'])) {
                    $noteCosMedia = explode(',', $item['update_json']['note_cos_media']);
                }
                $cosMedias = array_merge($cosMedias, $noteCosMedia);
            }
        }

        // cos media
        $cosMediaList = [];
        if (!empty($cosMedias)) {
            $cosMediaList = model(CosMediaModel::class)->where(['id' => ['IN', $cosMedias]])->select();
            $cosMediaList = array_column($cosMediaList, null, 'id');
            $cosMediaList = array_map(function ($tmp) {
                if (!empty($tmp['param'])) {
                    $tmp['param'] = json_decode($tmp['param'], true);
                }
                return $tmp;
            }, $cosMediaList);
        }
        // 普通 media
        $mediaList = [];
        if (!empty($medias)) {
            $mediaList = model(MediaModel::class)->where([
                'id' => ['IN', $medias]
            ])->field('id,thumb,param')->select();
            $mediaList = array_map(function ($mediaItem) {
                if (!empty($mediaItem['param'])) {
                    $mediaItem['param'] = json_decode($mediaItem['param'], true);
                }
                return $mediaItem;
            }, $mediaList);
            $mediaList = array_column($mediaList, null, 'id');
        }

        $userMap = array_column($userList, null, 'id');
        return array_map(function ($tmp) use ($userMap, $cosMediaList, $mediaList) {
            $updateJson = json_decode($tmp['update_json'], true);
            $noteMedia = [];
            $noteCosMedia = [];
            if (!empty($updateJson['note_cos_media'])) {
                $noteCosMediaArr = explode(',', $updateJson['note_cos_media']);
                foreach ($noteCosMediaArr as $tmpItem) {
                    if (!empty($cosMediaList[$tmpItem])) {
                        $noteCosMedia[] = $cosMediaList[$tmpItem];
                    }
                }
            }
            if (!empty($updateJson['note_media'])) {
                $noteMediaArr = explode(',', $updateJson['note_media']);
                foreach ($noteMediaArr as $tmpItem) {
                    if (!empty($mediaList[$tmpItem])) {
                        $noteMedia[] = $mediaList[$tmpItem];
                    }
                }
            }
            $tmp['note_media'] = $noteMedia;
            $tmp['note_cos_media'] = $noteCosMedia;
            $tmp['user_info'] = $userMap[$tmp['created_by']] ?? null;
            return $tmp;
        }, $noteRecordList);
    }
}
